package com.lepu.app.main.bean;

import java.util.ArrayList;
import java.util.HashMap;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;

public class BeanArea {

	public int Id;
	public String Name;
	public String Extra;
	
	public static BeanArea getAreaById(String id) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_CITY_NEW);
		if (dbHelp == null)
			return null;

		String sql = "select * from Location where Id=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] {id});

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		HashMap<String, Object> dictionary = queryResult.get(0);
		BeanArea item = BeanArea
				.parseFromDatabase(dictionary);
		return item;
	}

	public static ArrayList<BeanArea> getAreaByExtra(String extra) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_CITY_NEW);
		if (dbHelp == null)
			return null;

		String sql = "select * from Location where Extra=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] {extra});

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<BeanArea> dataList = new ArrayList<BeanArea>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			BeanArea item = BeanArea
					.parseFromDatabase(dictionary);
			dataList.add(item);
		}
		return dataList;
	}
	
	private static BeanArea parseFromDatabase(
			HashMap<String, Object> dictionary) {

		if (dictionary == null || dictionary.size() == 0) {
			return null;
		}
		BeanArea item = new BeanArea();

		item.Id = UtilityBase.parseInt((String) dictionary.get("Id"));
		item.Name = UtilityBase.parseString((String) dictionary.get("Name"));
		item.Extra = UtilityBase.parseString((String) dictionary.get("Extra"));

		return item;
	}
}
